package com.milks.filter;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.milks.dao.ProductDao;
import com.milks.entity.Product;
import com.milks.service.HtmlService;
import com.milks.service.ProductService;

public class HtmlInterceptor implements HandlerInterceptor {
	
	@Resource
	private HtmlService htmlService=null;
	@Resource
	private ProductService productService=null;
	@Resource
	private ProductDao productDao =null;
	
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse res,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse res,
			Object arg2) throws Exception {
	
		/*productService = new ProductServiceImpl();
		htmlService=new HtmlServiceImpl();
		productDaoImpl = new ProductDaoImpl();*/
		
		String contextPathString = req.getContextPath();
		String currentURL = req.getRequestURI();
		// 得到访问文件名. 例如 /index.jsp
		String targetURL = currentURL.substring(currentURL.lastIndexOf("/"),
				currentURL.length());
		// 得到htmlFilePath
		String htmlFilePath = currentURL.split(contextPathString)[1];
		// 对文件名进行拆分. 先得到/index 然后在得到index
		String splitName = targetURL.split("\\.")[0];
		String name = splitName.split("/")[1];
		String fileName = name + ".html";
		//String path = protDirPath + fileName;
		String path = fileName;
		// 判断文件是否存在. 存在的话则执行跳转. 否则执行生成.
		
		File file = new File(targetURL);
		if (!file.exists()) {
			System.out.println("生成HTML文件,执行跳转..");
			/*PrintWriter out = response.getWriter();
			FileCaptureResponseWrapper responseWrapper = new FileCaptureResponseWrapper(
					(HttpServletResponse) response);
			chain.doFilter(request, responseWrapper);
			String html = responseWrapper.toString();
			System.out.println(html);
			// 得到的html页面结果字符串
			responseWrapper.writeFile(path);
			responseWrapper.writeResponse(out);*/
			//ProductServiceImpl productService = new ProductServiceImpl();
			//Product product = productService.load(name);
			Product product2 = productDao.getProductByHtmlFilePath(htmlFilePath);
			//HtmlServiceImpl htmlService=new HtmlServiceImpl();
			/*Product product2 = new Product();
			product2.setHtmlFilePath(htmlFilePath);*/
			htmlService.productContentBuildHtml(product2);
			res.setHeader("REFRESH", "0;URL=" + req.getContextPath() + "/"
					+ targetURL);
		} else {
			System.out.println("目标文件已经存在,执行跳转...");
			res.setHeader("REFRESH", "0;URL=" + req.getContextPath() + "/"
					+ htmlFilePath);
		}
		
		return false;
	}
	
	
}
